\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$babel-lang$,$endif$$if(papersize)$$papersize$paper,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$}

\makeatletter
\newcommand\publisher[1]{\def\sjtu@value@publisher{#1}}
\renewcommand\maketitle{%
  \pdfbookmark[0]{\sjtu@titlepage}{titlepage}
  \makechinesetitle
}

\ifsjtu@english

%% labels in excercise, solution, and answer 
\def\sjtu@label@exercise{Exercise}
\def\sjtu@label@solution{Solution}
\def\sjtu@label@answerT{Answer}

\else

%% 练习，解答，答的标签
\def\sjtu@label@exercise{练习}
\def\sjtu@label@solution{解答}
\def\sjtu@label@answerT{答}

\fi

\makeatother

$if(title)$
\title{$title$}
$endif$
$if(englishtitle)$
\englishtitle{$englishtitle$}
$endif$
$if(author)$
\author{$author$}
$endif$
$if(keywords)$
\keywords{$keywords$}
$endif$
$if(publisher)$
\publisher{$publisher$}
$endif$
$if(date)$
\submitdate{$date$}
$endif$


\usepackage[export]{adjustbox}
\usepackage{etoc}
\usepackage{bookmark}

% 参考https://stackoverflow.com/questions/29696172/how-to-hold-figure-position-with-figure-caption-in-pdf-output-of-knitr
% http://cs.brown.edu/about/system/managed/latex/doc/float.pdf
\usepackage{float}
\newcommand\setdefaultfigure{\floatplacement{figure}{htp}}
\newcommand\setholdfigure{\floatplacement{figure}{H}}
\setdefaultfigure

% 为表格隔行涂色
\usepackage{colortbl}
\definecolor{Gray}{gray}{0.9}
\definecolor{LightCyan}{rgb}{0.88,1,1}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}m{#1}}

% 参考 ctexbook.cls 及
% http://www.latextemplates.com/template/the-legrand-orange-book
% https://tex.stackexchange.com/questions/48900/two-independent-tocs
\definecolor{ocre}{RGB}{243,102,25} % Define the orange color used for highlighting throughout the book

%----------------------------------------------------------------------------------------
%	为兼容 bookdown 引言模块添加的部分
%----------------------------------------------------------------------------------------
% 添加引言块
\def\VA#1#2{\addvspace{12pt}\raggedleft #1\rightskip3em\par #2\rightskip3em}
\renewenvironment{quote}
  {\list{}{\rightmargin\leftmargin}%
    \item\relax}
  {\endlist}

%----------------------------------------------------------------------------------------
%	书籍每一部分的设计，包括名言、解说和本部分目录
%----------------------------------------------------------------------------------------

\makeatletter

\newif\ifusepartquote
\usepartquotetrue
\newcommand{\thepartquote}{}
\newcommand{\thepartquoteauthor}{}
\newcommand{\partquote}[2]{\ifusepartquote\renewcommand{\thepartquote}{#1}\renewcommand{\thepartquoteauthor}{#2}\fi}

\newif\ifusepartintro
\usepartintrotrue
\newcommand{\thepartintro}{}
\newcommand{\partintro}[1]{\ifusepartintro\renewcommand{\thepartintro}{#1}\fi}
\newlength\ocreypos
\setlength\ocreypos{.35\paperheight}
% numbered part in the table of contents
\def\@part[#1]#2{%
  \ifnum \c@secnumdepth >-2\relax
    \ifodd \CTEX@part@numbering
      \CTEX@ifnametrue
      \refstepcounter{part}%
    \else
      \CTEX@ifnamefalse
      \CTEX@makeanchor{part*}%
    \fi
  \else
    \CTEX@ifnamefalse
    \CTEX@makeanchor{part*}%
  \fi
  \CTEX@addtocline{part}{#1}%
  \partmark{#1}%
  {\interlinepenalty \@M
   \normalfont \CTEX@part@format
   \ifnum \c@secnumdepth >-2\relax \ifodd \CTEX@part@numbering
     \CTEX@partname \CTEX@part@aftername
   \fi \fi}%
\markboth{}{}%
{\thispagestyle{empty}%
\begin{tikzpicture}[remember picture,overlay]%
\node at (current page.north west){\begin{tikzpicture}[remember picture,overlay]%	
\fill[ocre!20](0cm,0cm) rectangle (\paperwidth,-\paperheight);
\node[anchor=north] at (8cm,-3.75cm){\color{ocre!40}\fontsize{88}{40}\sffamily\bfseries\CTEX@partname};
\node[anchor=south east] at (\paperwidth-2cm,-\ocreypos+0.5cm){\color{ocre!40}\fontsize{44}{20}\sffamily\bfseries\CTEX@part@titleformat{#2}};
\fill[ocre!40](0cm,-\ocreypos) rectangle (\paperwidth,-\ocreypos-0.1cm);
\node[anchor=north west] at (2cm,-\ocreypos-0.5cm){\ifusepartquote\parbox[t][][t]{\paperwidth-4cm}{\begin{quote}
\quad\quad`` \thepartquote ''

\VA{------ \quad\thepartquoteauthor}{}
\end{quote}}\fi};
\node[anchor=south west] at (2cm,-\paperheight+2cm){\ifusepartintro\parbox[t][][t]{\paperwidth/2-2.5cm}{\thepartintro}\fi};
\node[anchor=south east] at (\paperwidth-2cm,-\paperheight+2cm){\parbox[t][][t]{\paperwidth/2-2.5cm}{
\etocsettocstyle{\renewcommand\contentsname{\null}}{}
\etocsetnexttocdepth{0} % 目录层次到章
\localtableofcontents
}};
\end{tikzpicture}};
\end{tikzpicture}}%
\@endpart}
\makeatother

%----------------------------------------------------------------------------------------
%	书籍每一部分的设计，包括名言、解说和本部分目录
%----------------------------------------------------------------------------------------

% 加入封面
$if(cover-image)$
\usepackage{wallpaper}
$endif$

\usepackage{booktabs}
\usepackage{longtable}
\usepackage{framed,color}
\definecolor{shadecolor}{RGB}{248,248,248}

\makeatletter
\newenvironment{kframe}{%
\medskip{}
\setlength{\fboxsep}{.8em}
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother
\newenvironment{Shaded}{\begin{kframe}}{\end{kframe}}

\makeatletter
\@ifundefined{Shaded}{
}{\renewenvironment{Shaded}{\begin{kframe}}{\end{kframe}}}
\makeatother
$for(bibliography)$
\addbibresource{$bibliography$}
$endfor$

\providecommand{\tightlist}{%
    \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

$if(href2footnote)$% 是否把链接改为脚注
\let\oldhref\href
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$

\usepackage{listings}
\lstset{mathescape=false}
% https://github.com/jgm/pandoc/issues/4716
% lstinline 对数学模式存在 bug，等修复后可以改为如下方式
\newcommand{\passthrough}[1]{#1}
% \newcommand{\passthrough}[1]{\lstset{mathescape=false}#1\lstset{mathescape=true}}

% https://tex.stackexchange.com/questions/30845/how-to-redefine-lstinline-to-automatically-highlight-or-draw-frames-around-all/30851
% https://www.spinics.net/lists/perfbook/msg00036.html
% another renewcommand/lstinline


% 感谢 AlexaraWu 的指导 https://github.com/sjtug/SJTUThesis/issues/343
% 兼容 Bookdown 定理等框架
% 重新定义定理环境
\usepackage{amsthm}
\makeatletter
\theoremstyle{plain}
  \newtheorem{theorem}{\sjtu@label@thm~}[chapter]
  \newtheorem{lemma}[theorem]{\sjtu@label@lem~}
  \newtheorem{proposition}[theorem]{\sjtu@label@prop~}
  \newtheorem{corollary}[theorem]{\sjtu@label@cor~}
\theoremstyle{definition}
  \newtheorem{definition}{\sjtu@label@defn~}[chapter]
  \newtheorem{exercise}{\sjtu@label@exercise}[chapter]
  \newtheorem{conjecture}{\sjtu@label@conj~}[chapter]
  \newtheorem{example}{\sjtu@label@exmp~}[chapter]
\theoremstyle{remark}
  \newtheorem{remark}{\sjtu@label@rem~}
  \newtheorem*{solution}{\bfseries{\sjtu@label@solution}}
\makeatother

\makeatletter
% Boxed/framed environments
\newtheoremstyle{ocrenumbox}% % Theorem style name
{0pt}% Space above
{0pt}% Space below
{\normalfont}% % Body font
{}% Indent amount
{\small\bf\sffamily\color{ocre}}% % Theorem head font
{\;}% Punctuation after theorem head
{0.25em}% Space after theorem head
{\small\sffamily\color{ocre}\thmname{#1}\nobreakspace\thmnumber{\@ifnotempty{#1}{}\@upn{#2}}% Theorem text (e.g. Theorem 2.1)
\thmnote{\nobreakspace\the\thm@notefont\sffamily\bfseries\color{black}---\nobreakspace#3.}} % Optional theorem note


% Defines the theorem text style for each type of theorem to one of the three styles above
\theoremstyle{ocrenumbox}
  \newtheorem{questionT}{}[chapter]
\theoremstyle{plain}
  \newtheorem*{answerT}{\sjtu@label@answerT}
\makeatother

%----------------------------------------------------------------------------------------
%	DEFINITION OF COLORED BOXES
%----------------------------------------------------------------------------------------

\RequirePackage[framemethod=default]{mdframed} % Required for question boxes

% Question box	  
\newmdenv[skipabove=7pt,
skipbelow=7pt,
rightline=false,
leftline=true,
topline=false,
bottomline=false,
backgroundcolor=ocre!10,
linecolor=ocre,
innerleftmargin=5pt,
innerrightmargin=5pt,
innertopmargin=5pt,
innerbottommargin=5pt,
leftmargin=0cm,
rightmargin=0cm,
linewidth=4pt]{qBox}	

% Creates an environment for each type of theorem and assigns it a theorem text style from the "Theorem Styles" section above and a colored box from above
% https://tex.stackexchange.com/questions/195774/how-to-right-align-any-line-or-word-in-a-paragraph-in-any-documentclass
% 实现不管是否换行都居右，建议所有的 \hfill 换成 \hspace*{\fill}
\newenvironment{question}{\begin{qBox}\begin{questionT}}{\end{questionT}\end{qBox}}	
\newenvironment{answer}{\begin{answerT}}{\hspace*{\fill}{\color{ocre}\ensuremath{\blacksquare}}\end{answerT}}%


% https://tex.stackexchange.com/questions/154570/itemize-environment-within-a-tabular-environment/154577
% 添加各类消息框
\newenvironment{rmdblock}[1]
  {
  \begin{table}[!htpb]
  \begin{tabular}{|cc|}
  \hline \quad \quad \quad &
  \begin{minipage}{.88\textwidth}
  \begin{itemize}
  \renewcommand{\labelitemi}{
    \raisebox{-.7\height}[0pt][0pt]{
      {\setkeys{Gin}{width=3em,keepaspectratio}\includegraphics{images/bookdown/#1}}
    }
  }
  \setlength{\fboxsep}{1em}
  \begin{kframe}
  \item
  }
  {
  \end{kframe}
  \end{itemize}
  \end{minipage}
  \\
  \hline
  \end{tabular}
  \end{table}
  }
\newenvironment{rmdnote}
  {\begin{rmdblock}{note}}
  {\end{rmdblock}}
\newenvironment{rmdcaution}
  {\begin{rmdblock}{caution}}
  {\end{rmdblock}}
\newenvironment{rmdimportant}
  {\begin{rmdblock}{important}}
  {\end{rmdblock}}
\newenvironment{rmdtip}
  {\begin{rmdblock}{tip}}
  {\end{rmdblock}}
\newenvironment{rmdwarning}
  {\begin{rmdblock}{warning}}
  {\end{rmdblock}}

% 解决 URL 过长问题
% https://stackoverflow.com/questions/2640111/url-latex-linebreak
% https://tex.stackexchange.com/questions/3033/forcing-linebreaks-in-url/10401
\Urlmuskip = 0mu plus 1mu
\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks% save the current one
  \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
  \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
  \do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D%
  \do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N%
  \do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X%
  \do\Y\do\Z\do\*\do\_\do\~\do\'\do\"\do\-}%

% 增加每章作者
\newcommand\writeby[1]{\rightline{------ #1}}

\makeatletter
% 实现附录按甲乙丙丁、子丑寅卯排序，但是仅限于二十二个以内。否则，报错。
% https://tex.stackexchange.com/questions/219711/definition-with-roman-counter-value
\newcommand\tiangani{甲}
\newcommand\tianganii{乙}
\newcommand\tianganiii{丙}
\newcommand\tianganiv{丁}
\newcommand\tianganv{戊}
\newcommand\tianganvi{己}
\newcommand\tianganvii{庚}
\newcommand\tianganviii{辛}
\newcommand\tianganix{壬}
\newcommand\tianganx{癸}
\newcommand\tianganxi{子}
\newcommand\tianganxii{丑}
\newcommand\tianganxiii{寅}
\newcommand\tianganxiv{卯}
\newcommand\tianganxv{辰}
\newcommand\tianganxvi{巳}
\newcommand\tianganxvii{午}
\newcommand\tianganxviii{未}
\newcommand\tianganxix{申}
\newcommand\tianganxx{酉}
\newcommand\tianganxxi{戌}
\newcommand\tianganxxii{亥}
\newcommand*\Tiangan[1]{\csname tiangan\romannumeral\value{#1}\endcsname}
\ifsjtu@english
\else
% https://latex.org/forum/viewtopic.php?t=2674
\let\oldappendix\appendix
\renewcommand\appendix{%
  \oldappendix
  \renewcommand{\CTEX@prechapter}{附录}
  \renewcommand{\CTEX@thechapter}{\Tiangan{chapter}}
  \renewcommand{\thechapter}{\Tiangan{chapter}}
}
\fi
\makeatother

% 增加索引功能
% https://tex.stackexchange.com/questions/144494/index-will-not-show-up-in-table-of-contents
% https://mirror.hmc.edu/ctan/macros/latex/contrib/imakeidx/imakeidx.pdf
% https://tex.stackexchange.com/questions/238321/indexing-numbers-of-different-types
\usepackage[texindy]{imakeidx}
% 采用中英文合并排序的方式
% https://tex.stackexchange.com/questions/147578/how-to-make-index-with-alphabetic-order
% https://tex.stackexchange.com/questions/199211/differences-between-xindy-and-makeindex
\makeindex[title=索引,columns=2,intoc=true,columnseprule=true]
% 下面用于解决页眉丢失问题。
% \usepackage{tocbibind} % 该功能导致页面混乱，最后删除。
% https://tex.stackexchange.com/questions/356094/index-and-fancyhydr
\indexsetup{
  othercode={%
    \thispagestyle{main}%
  }
}


\makeatletter
% 授权声明
\renewcommand\makeDeclareAuthorization{%
%----------------------------------------------------------------------------------------
%	COPYRIGHT PAGE
%----------------------------------------------------------------------------------------

\newpage

\thispagestyle{empty}

\noindent 版权 \copyright\ 2018 \sjtu@value@author\\ % Copyright notice

\noindent \textsc{\sjtu@value@publisher}\\ % Publisher

\noindent \textsc{\url{https://github.com/bubifengyun/deepin-bible}}\\ % URL

\noindent 版权采用 Creative Commons Attribution-NonCommercial 3.0 Unported License，内容见http://creativecommons.org/licenses/by-nc/3.0。共享版权，商业用途请联系我们。如果您采用六角丛书的价格发布图书，可以不用联系我们。\\ % License information

\noindent \textit{\sjtu@value@publisher 于 \sjtu@value@submitdate} % Printing/edition date
~\vfill
}

% “绘制”中文标题页
\renewcommand\makechinesetitle{%
  \cleardoublepage
  \thispagestyle{empty}
  \begin{center}
  \vskip\stretch{12}
  {\heiti\zihao{1}\sjtu@value@englishtitle}
  \vskip 20pt
  {\heiti\zihao{1}\sjtu@value@chinesetitle}
  \vskip 20pt
  {\fangsong\zihao{4}{\sjtu@value@author \quad\quad 编著}}
  \vskip\stretch{30}
  {\heiti\zihao{4}{\sjtu@value@publisher}}
  \end{center}

  ~\vfill
}

% Setting Package listings/lstlistings
\lstset{
  escapeinside={\%*}{*)}% 支持LaTeX代码入框 https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings
}

\graphicspath{{images/}}

\AtBeginDocument{
  \hypersetup{
    urlcolor=blue,
    linkcolor=blue,
    citecolor=blue,
  }
}
% fancyhdr页眉页脚设置
%% 研究生学位论文页眉页脚设置
  \if@twoside
  %% 双面打印页眉页脚
    \fancypagestyle{front}{%
      \fancyhf{}
      \fancyhead[LO,RE]{\small\kaishu\sjtu@value@chinesetitle}
      \fancyhead[LE,RO]{\nouppercase{\sjtu@fancyhead\leftmark}}
      \fancyfoot[C]{\small ---~{\bfseries\thepage}~---}
      \renewcommand{\headheight}{32pt}
    }
    %% 正文部分，奇数页页眉为书名章名，偶数页页眉为章名节名
    \fancypagestyle{main}{%
      \fancyhf{}
      \fancyhead[LO]{\small\kaishu\sjtu@value@chinesetitle}
      \fancyhead[RO,LE]{\nouppercase{\sjtu@fancyhead\leftmark}}
      \fancyhead[RE]{\nouppercase{\sjtu@fancyhead\rightmark}}
      \fancyfoot[C]{\small ---~{\bfseries\thepage}~---}
      \renewcommand{\headheight}{32pt}
    }
  \else
  %% 单面打印页眉页脚
    \fancypagestyle{front}{%
      \fancyhf{}
      \fancyhead[L]{\small\kaishu\sjtu@value@chinesetitle}
      \fancyhead[R]{\nouppercase{\sjtu@fancyhead\leftmark}}
      \fancyfoot[C]{\small ---~{\bfseries\thepage}~---}
      \renewcommand{\headheight}{32pt}
    }
    \fancypagestyle{main}{%
      \fancyhf{}
      \fancyhead[L]{\small\kaishu\sjtu@value@chinesetitle}
      \fancyhead[R]{\nouppercase{\sjtu@fancyhead\leftmark}}
      \fancyfoot[C]{\small ---~{\bfseries\thepage}~---}
      \renewcommand{\headheight}{32pt}
    }
  \fi

\makeatother

\begin{document}

$if(cover-image)$
  \cleardoublepage
  \thispagestyle{empty}
  \pdfbookmark[0]{封~~~~面}{cover-image}
  \begin{center}
    \ThisTileWallPaper{\paperwidth}{\paperheight}{$cover-image$}
  \end{center}
  \cleardoublepage
$endif$


% https://tex.stackexchange.com/questions/30312/shrink-figure-only-when-necessary
% https://stackoverflow.com/questions/1565988/making-a-small-modification-to-a-latex-environment
% https://tex.stackexchange.com/questions/116670/duplicating-environments
% 修复 Markdown 图片格式过大的问题。
% 需要\usepackage[export]{adjustbox}
\let\oldincludegraphics\includegraphics
\renewcommand\includegraphics[2][]{
\begin{adjustbox}{max size={\textwidth}{\textheight}}
\centering
\oldincludegraphics[#1]{#2}
\end{adjustbox}
}

\maketitle
\makeDeclareAuthorization

$body$

\backmatter	% 文后无编号部分 

% https://stackoverflow.com/questions/1483396/how-to-explicitly-end-a-part-in-latex-with-hyperref
% 把索引等从最后一个 PART 踢出去。
\bookmarksetup{startatroot}
% 索引
\printindex

%% 参考资料
\printbibliography[heading=bibintoc]
\end{document}
